﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace FireMan
{
    public partial class MainWindow : Form
    {
        public MainWindow()
        {
            startupInit();
            InitializeComponent();
            loadPluginList();
            populateFirmList();
        }

        // Public variables
        public dynamic string firmPath = "";
        






        // Button functions
        private void firmwareList_SelectedIndexChanged(object sender, EventArgs e)
        {
            TreeNode node = new TreeNode();
            node.Text = firmwareList.SelectedItem.ToString();
            node.Tag = Application.StartupPath + "\\firmware\\" + firmwareList.SelectedItem.ToString();
            populateDirView(Application.StartupPath + "\\firmware\\" + firmwareList.SelectedItem.ToString(), node);
            dirView.Nodes.Add(node);
            node.Expand();
            dirView.SelectedNode = node;
            populateListView(node.Tag.ToString());

        }

        private void toolStripCloseFirm_Click(object sender, EventArgs e)
        {
            dirView.Nodes.Clear();
            firmwareList.Items.Clear();
            populateFirmList();
        }

        private void aboutFireManToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new About().ShowDialog();
        }

        private void SearchButton_Click(object sender, EventArgs e)
        {
            if (firmwareList.SelectedItem == null)
            {
                MessageBox.Show("Error!\nNo open firmware!\nPlease open a firmware to use the search feature!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                DirectoryInfo rootDir = new DirectoryInfo(firmwareList.SelectedItem.ToString());
                foreach(DirectoryInfo dir in rootDir.GetDirectories(SearchBox.Text,SearchOption.AllDirectories))
                {
                    ListViewItem item = new ListViewItem();
                    item.Text = dir.Name;
                    item.Tag = dir.FullName;
                    listView.Items.Add(item);
                }
                foreach(FileInfo file in rootDir.GetFiles(SearchBox.Text,SearchOption.AllDirectories))
                {
                    ListViewItem item = new ListViewItem();
                    item.Text = file.Name;
                    item.Tag = file.FullName;
                    listView.Items.Add(item);
                }
            }
        }

        private void openFirmwareToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openFirm();
        }

        private void toolStripUpdate_Click(object sender, EventArgs e)
        {
            dirView.Nodes.Clear();
            listView.Items.Clear();
            firmwareList.Items.Clear();
            populateFirmList();
        }

        private void toolStripOpenFirm_Click(object sender, EventArgs e)
        {
            openFirm();
        }

        private void dirView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            populateListView(dirView.SelectedNode.Tag.ToString());
        }

        private void donateToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=PK45FDP4EEN94&lc=HU&item_name=Phoenix%20Studios&amount=500&currency_code=HUF&currency_code=HUF&bn=PP%2dDonationsBF%3abtn_donate_LG%2egif%3aNonHosted");
        }

        private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new Settings().ShowDialog();
        }

        private void Settings_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            openFirmDialog.InitialDirectory = firmPath;
        }







        // Other functions
        private void populateDirView(string folder,TreeNode node)
        {
            dirView.Nodes.Clear();
            DirectoryInfo dir = new DirectoryInfo(folder);
            foreach (DirectoryInfo d in dir.GetDirectories())
            {
                TreeNode nodeItem = new TreeNode();
                nodeItem.Text = d.Name;
                nodeItem.Tag = d.FullName;
                populateDirView(d.FullName, nodeItem);
                node.Nodes.Add(nodeItem);
            }
        }

        private void populateFirmList()
        {
            firmwareList.Items.Clear();
            if (Directory.Exists(Application.StartupPath + "\\firmware\\"))
            {
                DirectoryInfo dir = new DirectoryInfo(Application.StartupPath + "\\firmware\\");
                int i = 0;
                foreach (DirectoryInfo d in dir.GetDirectories())
                {
                    firmwareList.Items.Insert(i, d.Name);
                    i++;
                }
            }
        }

        private void populateListView(string rootfolder)
        {
            listView.Items.Clear();
            DirectoryInfo dir = new DirectoryInfo(rootfolder);
            foreach (DirectoryInfo folder in dir.GetDirectories())
            {
                ListViewItem item = new ListViewItem();
                item.Text = "[" + folder.Name + "]";
                item.Tag = folder.FullName;
                listView.Items.Add(item);
            }
            foreach (FileInfo file in dir.GetFiles())
            {
                ListViewItem item = new ListViewItem();
                item.Text = file.Name;
                item.Tag = file.FullName;
                listView.Items.Add(item);
            }
        

            
        }

        private void loadPluginList()
        {
            DirectoryInfo dir = new DirectoryInfo(Application.StartupPath + "\\plugins\\");
            FileInfo[] plugins = dir.GetFiles("*.dll");
            if (plugins.Length == 0)
            {
                ToolStripMenuItem item = new ToolStripMenuItem();
                item.Text = "[NO PLUGINS]";
                item.Enabled = false;

                pluginsToolStripMenuItem.DropDownItems.Insert(0, item);
            }
            else
            {
                int i = 0;
                foreach (FileInfo plugin in plugins)
                {
                    ToolStripMenuItem item = new ToolStripMenuItem(plugin.Name);
                    pluginsToolStripMenuItem.DropDownItems.Insert(i,item);
                    i++;
                }
            }
        }

        private void openFirm()
        {
            if (openFirmDialog.ShowDialog() == DialogResult.OK)
            {
                Directory.CreateDirectory(Application.StartupPath + "\\firmware\\" + Path.GetFileName(openFirmDialog.FileName));
                populateFirmList();
            }
        }

        private void startupInit()
        {
            checkDir("firmware");
            checkDir("plugins");
            checkDir("temp");
        }

        private void checkDir(string dirName)
        {
            if (!Directory.Exists(Application.StartupPath + "\\" + dirName + "\\"))
            {
                Directory.CreateDirectory(Application.StartupPath + "\\" + dirName + "\\");
            }
        }
    }
}
